<template>
	<view>
		<wzk-examine :list="list" :type="type" @confirmSubimt='confirmSubimt'></wzk-examine>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				postId: '',
				list: [],
				sTime: '', //开始时间
				eTime: '' ,//结束时间
				type:1,
				currId:0
			}
		},
		onShow() {
			uni.hideHomeButton()
		},
		onLoad(option) {
			uni.showLoading({
				title: '请稍等...',
				mask: true
			})
			this.postId = option.postId || '18'
			this.type = option.type || 1
			if( option.type){
				this.type = option.type || 1
				this.currId = option.currId || 0
			}
			this.getList();

			this.sTime = (Date.parse(new Date()) / 1000)
			console.log(this.sTime);
		},
		methods: {
			getList() {
				if (this.type == 1) {
					this.getSchoolText();
				}else if(this.type==2){
					this.getCurrList();
				}
			},
			getSchoolText() {
				this.$http.get('app/preschoolController/selectPreschoolList', {
					postId: this.postId
				}, true, true).then(res => {
					if (res.code == 200) {
						uni.showLoading({
							title: '请稍等...',
							mask: true
						})
						res.data.forEach((e, index) => {
							if (e) {
								e.selectText = ''
								e.type = e.types
								if (e.types == 1 || e.types == 2) {
									e.title = e.subjectname
									e.selectA = e.optiona
									e.selectB = e.optionb
									e.selectC = e.optionc
									e.selectD = e.optiond
									e.confirmSelect = e.correctoption
								} else {
									e.title = e.subjectname
									e.confirmSelect = e.correctoption == '正确' ? 'true' : 'false'
								}
								this.list.push(e)
							}
							if ((index + 1) >= res.data.length) {
								uni.hideLoading();
							}
						})
					} else {
						uni.hideLoading();
						this.$mHelper.toast(res.message);
					}
				}).catch(err => {
					uni.hideLoading();
				})
			},
			getCurrList(){
					this.$http.get('app/preschoolController/selectCurrPaperList', {
						currId: this.currId
					}, true, true).then(res => {
						if (res.code == 200) {
							uni.showLoading({
								title: '请稍等...',
								mask: true
							})
							res.data.forEach((e, index) => {
								if (e) {
									e.selectText = ''
									e.type = e.types
									if (e.types == 1 || e.types == 2) {
										e.title = e.subjectname
										e.selectA = e.optiona
										e.selectB = e.optionb
										e.selectC = e.optionc
										e.selectD = e.optiond
										e.confirmSelect = e.correctoption
									} else {
										e.title = e.subjectname
										e.confirmSelect = e.correctoption == '正确' ? 'true' : 'false'
									}
									this.list.push(e)
								}
								if ((index + 1) >= res.data.length) {
									uni.hideLoading();
								}
							})
						} else {
							uni.hideLoading();
							this.$mHelper.toast(res.message);
						}
					}).catch(err => {
						uni.hideLoading();
					})
			},
			confirmSubimt(e) {
				if (!e.length) {
					this.$mHelper.toast('题目数据为空')
					return;
				}
				let param = {
					rightNum: 0, //答对数
					wrongNum: 0, //错题数
					totalScore: 0, //总分
					correct: 0, //正确率
					times: '' //答题用时
				}
				let numOne = parseInt(100 / e.length)

				let topic = []
				e.forEach((res, index) => {
					if (res.confirmSelect == res.selectText) {
						param.rightNum++;
						param.totalScore += numOne;
					} else {
						param.wrongNum++;
					}
					console.log( res.confirmSelect);
					console.log( res.selectText);
					topic.push({
						id: res.id,
						answers: res.selectText,
						corrects: res.confirmSelect == res.selectText,
						tQuestionBank: null
					})
					if ((index + 1) >= e.length) {
						this.allSubimt(e, param, topic);
					}

				})

			},
			allSubimt(e, param, topic) {
				param.correct = parseInt((param.rightNum / e.length) * 100)
				this.eTime = (Date.parse(new Date()) / 1000)
				param.times = parseInt((this.eTime - this.sTime) / 60)
				let data = {
					correctrate: param.correct,
					fractions: param.totalScore,
					timenum: param.times || 1,
					wronglist: JSON.stringify(topic),
					totalnum: e.length,
					wrongNum: param.wrongNum,
					rightNum: param.rightNum
				}
				let url=""
				if(this.type==1){
					url='app/preschoolController/addTQuestionsNum'
					data.postid= this.postId;
					this.types= 1;
				}else{
					url='app/preschoolController/addTcurriculumpaper'
					data.currid=this.currId
					this.types= 2;
				}
				this.$http.post(url, data).then(res => {
					if (res.code == 200) {
						uni.setStorage({
							key: 'results',
							data: data,
							success: (e) => {
								if(this.type==1){
									uni.redirectTo({
										url: '/pages/examine/existResults'
									})
								}else{
									uni.redirectTo({
										url: '/pages/examine/courserResult'
									})
								}
								
							},

						})
					} else {
						this.$mHelper.toast(res.message)
					}
				})
			},
		}
	}
</script>

<style>

</style>
